#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "gpio.h"
#include "time.h"
#include "adc.h"

int main(void)
{
	struct UartData* data;
	uint8_t voice_flag1=0;
	uint8_t turn_flag=0;
	uint8_t esc=0;
	uint8_t ent=0;
	uint8_t sort=1;
	uint8_t mode=1;
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	delay_init();
	uart_init(115200);
	GpioInit();
	TIME_Init();
	OLED_Init();
	OLED_CLS();
	ADC1_Init();
	printf("AT\r\n");
	Tim3_delay_ms(20);
	while(USART_RX_STA)
	{
		data=first;
		uint8_t buf[4]={0};
		while(first)
		{	
			sprintf((char*)buf,"%c",first->USART_RX_BUF);
			printf2(buf);
			first=first->next;
		}
		first=data;
		if(is_ok(first)){
			usart_free(data);
			USART_RX_STA=0;
			break;
		}
		usart_free(data);
		USART_RX_STA=0;
		printf("AT\r\n");
		Tim3_delay_ms(50);
	}
	printf("AT+CWMODE=2\r\n");
	Tim3_delay_ms(60);
	while(USART_RX_STA)
	{
		data=first;
		uint8_t buf[4]={0};
		while(first)
		{	
			sprintf((char*)buf,"%c",first->USART_RX_BUF);
			printf2(buf);
			first=first->next;
		}
		first=data;
		if(is_ok(first)){
			usart_free(data);
			USART_RX_STA=0;
			break;
		}
		usart_free(data);
		USART_RX_STA=0;
		printf("AT+CWMODE=2\r\n");
		Tim3_delay_ms(50);
	}
	printf("AT+CWSAP=\"智能管家\",\"12345678\",1,4\r\n");
	Tim3_delay_ms(60);
	while(USART_RX_STA)
	{
		data=first;
		uint8_t buf[4]={0};
		while(first)
		{	
			sprintf((char*)buf,"%c",first->USART_RX_BUF);
			printf2(buf);
			first=first->next;
		}
		first=data;
		if(is_ok(first)){
			usart_free(data);
			USART_RX_STA=0;
			break;
		}
		usart_free(data);
		USART_RX_STA=0;
		printf("AT+CWSAP=\"智能管家\",\"12345678\",1,4\r\n");
		Tim3_delay_ms(50);
	}
	printf("AT+CIPMUX=1\r\n");
	Tim3_delay_ms(60);
	while(USART_RX_STA)
	{
		data=first;
		uint8_t buf[4]={0};
		while(first)
		{	
			sprintf((char*)buf,"%c",first->USART_RX_BUF);
			printf2(buf);
			first=first->next;
		}
		first=data;
		if(is_ok(first)){
			usart_free(data);
			USART_RX_STA=0;
			break;
		}
		usart_free(data);
		USART_RX_STA=0;
		printf("AT+CIPMUX=1\r\n");
		Tim3_delay_ms(50);
	}
	printf("AT+CIPSERVER=1,8086\r\n");
	Tim3_delay_ms(60);
	while(USART_RX_STA)
	{
		data=first;
		uint8_t buf[4]={0};
		while(first)
		{	
			sprintf((char*)buf,"%c",first->USART_RX_BUF);
			printf2(buf);
			first=first->next;
		}
		first=data;
		if(is_ok(first)){
			usart_free(data);
			USART_RX_STA=0;
			break;
		}
		usart_free(data);
		USART_RX_STA=0;
		printf("AT+CIPSERVER=1,8086\r\n");
		Tim3_delay_ms(50);
	}
	while(DHT11_Init())
	{
		for(uint8_t i=0;i<10;i++)
		{
			Tim3_delay_ms(40);
		}
		printf2((uint8_t*)"DHT11 Init fail\r\n");
	}
    while(1)
    {
		switch(key_scan()){
			case 1:
				mode=1;
				sort=1;
				break;
			case 2:
				sort++;
				if(sort>3){sort=1;}
				break;
			case 3:
				sort--;
				if(sort<1){sort=3;}
				break;
			case 4:
				mode=sort+1;
				sort=1;
				break;
			default:
				break;
		}
		if(mode==1&&sort==1)
		{
			buzzer_off();
			uint8_t buf[30]={0};
			sprintf((char*)buf,"智能管家     ");
			OLED_ShowStr(32,0,buf);
			sprintf((char*)buf,"自动模式  <--  ");
			OLED_ShowStr(0,2,buf);
			sprintf((char*)buf,"手动模式       ");
			OLED_ShowStr(0,4,buf);
			sprintf((char*)buf,"儿童模式       ");
			OLED_ShowStr(0,6,buf);
		}else if(mode==1&&sort==2){
			uint8_t buf[30]={0};
			sprintf((char*)buf,"智能管家       ");
			OLED_ShowStr(32,0,buf);
			sprintf((char*)buf,"自动模式       ");
			OLED_ShowStr(0,2,buf);
			sprintf((char*)buf,"手动模式  <--  ");
			OLED_ShowStr(0,4,buf);
			sprintf((char*)buf,"儿童模式       ");
			OLED_ShowStr(0,6,buf);
		}else if(mode==1&&sort==3){
			uint8_t buf[30]={0};
			sprintf((char*)buf,"智能管家       ");
			OLED_ShowStr(32,0,buf);
			sprintf((char*)buf,"自动模式       ");
			OLED_ShowStr(0,2,buf);
			sprintf((char*)buf,"手动模式       ");
			OLED_ShowStr(0,4,buf);
			sprintf((char*)buf,"儿童模式  <--  ");
			OLED_ShowStr(0,6,buf);
		}
		if(mode==2)
		{
			uint8_t buf[30]={0};
			uint8_t tem=0;
			uint8_t hum=0;
			uint16_t light=Get_Adc(ADC_Channel_4);
			uint16_t smoke=Get_Adc(ADC_Channel_5);
			float light1=(1-light/4095.0)*100;
			float smoke1=smoke/40.96;
			DHT11_Read_Data(&tem,&hum);
			uint8_t rain=Get_Adc(ADC_Channel_6);
			uint8_t light_flag=get_light_level();
			uint8_t hot_flag=get_hot_level();
			uint8_t smoke_flag=get_smoke_level();
			if(mode==2&&sort==1){
				sprintf((char*)buf,"自动模式  ");
				OLED_ShowStr(32,0,buf);
				sprintf((char*)buf,"温度:%d 湿度:%d",tem,hum);
				OLED_ShowStr(0,2,buf);
				sprintf((char*)buf,"光照强度:%.2f%%",light1);
				OLED_ShowStr(0,4,buf);
				sprintf((char*)buf,"空气指数:%.2f%%",smoke1);
				OLED_ShowStr(0,6,buf);
			}else if(mode==2&&sort==2){
				sprintf((char*)buf,"自动模式  ");
				OLED_ShowStr(32,0,buf);
				if(rain==255)
				{
					sprintf((char*)buf,"雨水:无        ");
					OLED_ShowStr(0,2,buf);
				}else if(150<rain){
					sprintf((char*)buf,"雨水:小雨       ");
					OLED_ShowStr(0,2,buf);
				}else if(50<rain){
					sprintf((char*)buf,"雨水:中雨       ");
					OLED_ShowStr(0,2,buf);
				}else if(0<=rain){
					sprintf((char*)buf,"雨水:大雨       ");
					OLED_ShowStr(0,2,buf);
				}
				if(light_flag==1){
					sprintf((char*)buf,"夜晚:警报器打开 ");
					OLED_ShowStr(0,4,buf);
				}else if(light_flag==0){
					sprintf((char*)buf,"白天:警报器关闭 ");
					OLED_ShowStr(0,4,buf);
				}
				if(hot_flag==1){
					sprintf((char*)buf,"有人靠近:      ");
					OLED_ShowStr(0,6,buf);
				}else if(hot_flag==0){
					sprintf((char*)buf,"无人靠近:      ");
					OLED_ShowStr(0,6,buf);
				}
			}else if(mode==2&&sort==3){
				sprintf((char*)buf,"自动模式  ");
				OLED_ShowStr(32,0,buf);
				if(turn_flag==1){
					sprintf((char*)buf,"窗户打开       ");
					OLED_ShowStr(0,2,buf);
				}else if(turn_flag==0){
					sprintf((char*)buf,"窗户关闭       ");
					OLED_ShowStr(0,2,buf);
				}
				sprintf((char*)buf,"功能添加中...   ");
				OLED_ShowStr(0,4,buf);
				sprintf((char*)buf,"功能添加中...   ");
				OLED_ShowStr(0,6,buf);
			}
			if((light_flag==1&&hot_flag==1)||smoke_flag==0){
				buzzer_on();
			}else{
				buzzer_off();
			}
			if(rain==255&&smoke_flag==1&&light_flag==0)
			{
				if(turn_flag==1)
				{
				}else if(turn_flag==0)
				{
					step_on();
					turn_flag=1;
				}
			}else if(rain<255||smoke_flag==0||light_flag==1)
			{
				if(turn_flag==1)
				{
					step_off();
					turn_flag=0;
				}else if(turn_flag==0)
				{
				}
			}
		}else if(mode==3){
			uint8_t buf[30]={0};
			if(turn_flag==1&&sort==1){
				sprintf((char*)buf,"手动模式  ");
				OLED_ShowStr(32,0,buf);
				sprintf((char*)buf,"打开窗户     ");
				OLED_ShowStr(0,2,buf);
				sprintf((char*)buf,"关闭窗户  <--");
				OLED_ShowStr(0,4,buf);
				sprintf((char*)buf,"窗户状态: 打开");
				OLED_ShowStr(0,6,buf);
			}else if(turn_flag==0&&sort==1){
				sprintf((char*)buf,"手动模式  ");
				OLED_ShowStr(32,0,buf);
				sprintf((char*)buf,"打开窗户  <--");
				OLED_ShowStr(0,2,buf);
				sprintf((char*)buf,"关闭窗户     ");
				OLED_ShowStr(0,4,buf);
				sprintf((char*)buf,"窗户状态: 关闭");
				OLED_ShowStr(0,6,buf);
			}else if(turn_flag==1&&sort==2){
				sprintf((char*)buf,"手动模式  ");
				OLED_ShowStr(32,0,buf);
				sprintf((char*)buf,"打开窗户  <--");
				OLED_ShowStr(0,2,buf);
				sprintf((char*)buf,"关闭窗户     ");
				OLED_ShowStr(0,4,buf);
				sprintf((char*)buf,"窗户状态: 关闭");
				OLED_ShowStr(0,6,buf);
				turn_flag=0;
				sort=1;
				step_off();
			}else if(turn_flag==0&&sort==2){
				sprintf((char*)buf,"手动模式  ");
				OLED_ShowStr(32,0,buf);
				sprintf((char*)buf,"打开窗户  <--");
				OLED_ShowStr(0,2,buf);
				sprintf((char*)buf,"关闭窗户     ");
				OLED_ShowStr(0,4,buf);
				sprintf((char*)buf,"窗户状态: 打开");
				OLED_ShowStr(0,6,buf);
				turn_flag=1;
				sort=1;
				step_on();
			}
		}else if(mode==4){
			uint8_t buf[30]={0};
			uint8_t wifi_flag=2;
			uint8_t voice_flag=get_voice_level();	
			sprintf((char*)buf,"儿童模式  ");
			OLED_ShowStr(32,0,buf);
			if(USART_RX_STA){
				data=first;
				uint8_t buf1[4]={0};
				while(first)
				{	
					sprintf((char*)buf1,"%c",first->USART_RX_BUF);
					printf2(buf1);
					first=first->next;
				}
				first=data;
				wifi_flag=is_ipd(first);
				usart_free(data);
				if(wifi_flag==1){	
					if(turn_flag==1){
					}else if(turn_flag==0){
						turn_flag=1;
						step_on();
					}
				}else if(wifi_flag==0){
					if(turn_flag==1){
						turn_flag=0;
						step_off();
					}else if(turn_flag==0){	
					}
				}
				USART_RX_STA=0;
			}else{
				if(voice_flag!=voice_flag1){
					if(voice_flag==1)
					{
						if(turn_flag==1){
						}else if(turn_flag==0){
							turn_flag=1;
							step_on();
						}
					}else if(voice_flag==0){
						if(turn_flag==1){
							turn_flag=0;
							step_off();
						}else if(turn_flag==0){	
						}
					}
				}
				voice_flag1=voice_flag;
			}
			if(turn_flag==1){
				sprintf((char*)buf,"窗户状态: 打开");
				OLED_ShowStr(0,2,buf);
			}else if(turn_flag==0){
				sprintf((char*)buf,"窗户状态: 关闭");
				OLED_ShowStr(0,2,buf);
			}
			OLED_ShowStr(0,2,buf);
			sprintf((char*)buf,"             ");
			OLED_ShowStr(0,4,buf);
			sprintf((char*)buf,"             ");
			OLED_ShowStr(0,6,buf);
		}
    }
}


void test1(void)
{
	uint8_t buf[20]={0};
	sprintf((char*)buf,"%d,%d,%d,%d\r\n",Get_Adc(ADC_Channel_4),Get_Adc(ADC_Channel_5),Get_Adc(ADC_Channel_6),Get_Adc(ADC_Channel_7));
	printf2(buf);
}

void test2(void)
{
	uint8_t buf[10]={0};
	sprintf((char*)buf,"%d,%d,%d\r\n",get_smoke_level(),get_light_level(),get_rain_level());
	printf2(buf);
}
void test3(void)
{
	uint8_t buf[10]={0};uint8_t tem=0;uint8_t hum=0;
	if(!DHT11_Read_Data(&tem,&hum))
	{
		sprintf((char*)buf,"%d,%d\r\n",tem,hum);
		printf2(buf);
		for(uint8_t i=0;i<10;i++)
		{
			Tim3_delay_ms(40);
		}
	}
}
void test4(void)
{
	uint8_t buf[12]={0};
	sprintf((char*)buf,"%d\r\n",key_scan());
	printf2(buf);
	sprintf((char*)buf,"%d,%d,%d.%d\r\n",GPIO_ReadInputDataBit(GPIOA,KEY1),GPIO_ReadInputDataBit(GPIOA,KEY2)\
	,GPIO_ReadInputDataBit(GPIOA,KEY3),GPIO_ReadInputDataBit(GPIOA,KEY4));
	printf2(buf);
	Tim3_delay_ms(60);
}
void test5(void)
{
	uint8_t buf[30]={0};
	sprintf((char*)buf,"智能管家123");
	OLED_ShowStr(32,3,buf);
}